package cj.web.marriage.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.sms.SendSMSUtil;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.MarriageRegSetMealEnum;
import cj.web.marriage.constants.MarriageRegStatusEnum;
import cj.web.marriage.constants.MarriageRegTypeEnum;
import cj.web.marriage.constants.RegAddressEnum;
import cj.web.marriage.dao.MarriageRegDAO;
import cj.web.marriage.dao.RefAddressAuthoDAO;
import cj.web.marriage.domain.MarriageReg;

@Path("ceremonyref")
public class CeremonyRegController extends BaseController{
	
	private static Logger logger = Logger.getLogger(CeremonyRegController.class.getName());

	@Autowired
	private MarriageRegDAO marriageRegDAO;
	@Autowired
	private RefAddressAuthoDAO refAddressAuthoDAO;
	
	@Get("show")
	public String show(Model model,MarriageReg marriageReg)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.ceremonyref);
		model.add("AddressEnum",EnumSet.allOf(RegAddressEnum.class));
		model.add("StatusEnum",EnumSet.allOf(MarriageRegStatusEnum.class));
		model.add("TypeEnum",EnumSet.allOf(MarriageRegTypeEnum.class));
		
		model.add("setMeals",EnumSet.allOf(MarriageRegSetMealEnum.class));
		
		return "/views/marriage/ceremonyRegList.vm";
	}
	
	@Post("getList")
	public String queryList(MarriageReg marriageReg,HttpServletRequest request) {
		
		//登录用户ID
		int userId = returnSessionObj(request).getId();
		//查询地址权限
		List<Integer> addressIdlis = refAddressAuthoDAO.queryByUserId(userId);
		
		JSONObject jo = new JSONObject();
		List<MarriageReg> objs = new ArrayList<MarriageReg>();
		int size = 0;
		if(addressIdlis!=null && addressIdlis.size()>0)
		{
			objs = marriageRegDAO.queryCeremony(marriageReg,addressIdlis);
			
			size = marriageRegDAO.queryCeremonySize(marriageReg,addressIdlis);
		}
		
		jo.put("data", objs);
		jo.put("draw", marriageReg.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.ceremonyref);
		model.add("AddressEnum",EnumSet.allOf(RegAddressEnum.class));
		model.add("TypeEnum",EnumSet.allOf(MarriageRegTypeEnum.class));
		
		model.add("setMeals",EnumSet.allOf(MarriageRegSetMealEnum.class));
		
		return "/views/marriage/ceremonyRegDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,MarriageReg marriageReg,HttpServletRequest request)
	{
		marriageReg.setStatus(MarriageRegStatusEnum.yuyue.getValue());
		marriageReg.setType(MarriageRegTypeEnum.tesebanzheng.getValue());//颁证仪式类型
		
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		int repeat = marriageRegDAO.repeatCeremony(marriageReg);
		if(repeat > 0){
			jo.put("returnFlag", Constants.returnObj.returnFlag_repeat);
			jo.put("man", marriageReg.getMan());
			jo.put("link", marriageReg.getLink());
			
			return "@"+jo.toString();
		}
		
		Date date = DateUtils.parse(marriageReg.getRegTime(),DateUtils.DATE_SMALL_STR);
		marriageReg.setRegTime(DateUtils.getDateTime2Str(date, DateUtils.DATE_SMALL_STR));
		
		try {
			marriageRegDAO.create(marriageReg);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") String id)
	{
		model.add("AddressEnum",EnumSet.allOf(RegAddressEnum.class));
		model.add("StatusEnum",EnumSet.allOf(MarriageRegStatusEnum.class));
		model.add("TypeEnum",EnumSet.allOf(MarriageRegTypeEnum.class));
		
		model.add("setMeals",EnumSet.allOf(MarriageRegSetMealEnum.class));
		
		MarriageReg obj = marriageRegDAO.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.ceremonyref);
		return "/views/marriage/ceremonyRegEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,MarriageReg marriageReg,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		Date date = DateUtils.parse(marriageReg.getRegTime(),DateUtils.DATE_SMALL_STR);
		marriageReg.setRegTime(DateUtils.getDateTime2Str(date, DateUtils.DATE_SMALL_STR));
		
		try {
			marriageRegDAO.updateCeremony(marriageReg);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("finish")
	public String finish(@Param("id") String id) {
		MarriageReg obj = marriageRegDAO.queryById(id);

		if (obj != null) {
			obj.setStatus(MarriageRegStatusEnum.agree.getValue());
			marriageRegDAO.finish(obj);
		}

		return "r:show";
	}
	
	@Get("refusePage")
	public String refusePage(Model model,@Param("id") String id) throws ParseException
	{
		model.add("AddressEnum",EnumSet.allOf(RegAddressEnum.class));
		model.add("StatusEnum",EnumSet.allOf(MarriageRegStatusEnum.class));
		model.add("TypeEnum",EnumSet.allOf(MarriageRegTypeEnum.class));
		
		model.add("setMeals",EnumSet.allOf(MarriageRegSetMealEnum.class));
		
		MarriageReg obj = marriageRegDAO.queryById(id);
		
		if(obj!=null){
			obj.setRegTime(DateUtils.formatDateStr(obj.getRegTime(), DateUtils.DATE_SMALL_STR));
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.ceremonyref);
		return "/views/marriage/marriageRegRefusePage.vm";
	}
	
	@Get("refuse")
	public String refuse(@Param("id") String id,@Param("status") String status,
			@Param("refuseStatus") String refuseStatus,@Param("other") String other,@Param("agreeOther") String agreeOther) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			MarriageReg obj = marriageRegDAO.queryById(id);

			if (obj != null) {
				String memo="";
				if(status.equals("2")){
					obj.setStatus(MarriageRegStatusEnum.agree.getValue());
					memo=agreeOther;
				}else if(status.equals("3")){
					obj.setStatus(MarriageRegStatusEnum.refuse.getValue());
					if(refuseStatus.equals("1")){
						memo="预约时间不在工作日";
					}else if(refuseStatus.equals("2")){
						memo="信息填写不正确";
					}else if(refuseStatus.equals("3")){
						memo=other;
					}
				}
				
				memo = memo.replace(">", "&gt;");
				memo = memo.replace("<", "&lt;");
				obj.setMemo(memo);
				marriageRegDAO.refuse(obj);
				
				String phone = obj.getLink();
				String tempid = "104894";
				String[] msg = new String[]{memo};
				SendSMSUtil.sendSMS(accountSid, accountToken, appId, phone, tempid, msg);
			}
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}

		return "@"+jo.toString();
	}
}
